package com.tyg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tyg.entity.UserInfo;
import com.tyg.mapper.UserInfoMapper;
import com.tyg.service.UserInfoService;
import com.tyg.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工作人员表 前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@RestController
@RequestMapping("/user-info")
@CrossOrigin
@Slf4j
public class UserInfoController {
    @Autowired
    UserInfoMapper userMapper;

    @Autowired
    UserInfoService userInfoService;

    //登录功能
    @PostMapping("/login")
    public ResultCode getUserLogin(String workName, String password) {
        System.out.println(workName + "---" + password);
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
        queryWrapper.eq("delflag",0);
        queryWrapper.eq("workName",workName);
        queryWrapper.eq("password",password);
//        UserInfo user=userMapper.selectOne(queryWrapper);
        UserInfo user=userInfoService.selectOne(queryWrapper);

        if(!ObjectUtils.isEmpty(user)) {
            return ResultCode.success(user);
        }else {
            return ResultCode.error(500, "登陆失败！用户名或密码错误：");
        }
    }


    //模糊查询用户
    @PostMapping("/getUserList")
    public ResultCode getUserList(
            String workName,
            String workTel,
            String workSex,
            Integer roleId,
            @RequestParam(defaultValue = "1" ,required = true)int pageIndex, int pageSize) {
        System.out.println("workName:"+workName);
        System.out.println("workTel:"+workTel);
        System.out.println("workSex:"+workSex);
        System.out.println("roleId:"+roleId);
        PageHelper.startPage(pageIndex, pageSize);
        Map<String,Object> map= new HashMap<>();
        map.put("workName", workName);
        map.put("workTel", workTel);
        map.put("workSex", workSex);
        map.put("roleId",roleId);
//        List<UserInfo> userList= userMapper.getUserList(map);
        List<UserInfo> userList=userInfoService.getUserList(map);
        PageInfo<UserInfo> page= new PageInfo<UserInfo>(userList);
        return ResultCode.success(page);
    }

    //添加用户(未加身份证校验)
    @PostMapping("/addUser")
    public ResultCode addUser(@RequestBody UserInfo user) throws IllegalStateException {
        System.out.println(user);
        try {
//            userMapper.insert(user);
            userInfoService.addNewUser(user);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "添加失败！");
        }
    }

    //根据workId查询具体用户信息
    @PostMapping("/getUserById")
    public ResultCode getUserById(int workId) {
//        UserInfo user=userMapper.selectById(workId);
        UserInfo user=userInfoService.selectById(workId);
        return ResultCode.success(user);
    }

    //修改用户信息
    @PostMapping("/updateUser")
    public ResultCode updateUser(@RequestBody UserInfo user){
        try {
            System.out.println(user);
//            userMapper.updateById(user);
            userInfoService.updateById(user);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "修改失败！");
        }

    }

    //删除用户
    @PostMapping("/delUserById")
    public ResultCode delUserById(int workId) {

        try {
            UserInfo user=new UserInfo();
            user.setWorkId(workId);
            user.setDelFlag(1);
//            userMapper.updateById(user);
            userInfoService.updateById(user);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "删除失败！");
        }
    }

    //更新用户角色
    @PostMapping("/editUserRole")
    public ResultCode editUserRole(int workId,int roleId) {
        System.out.println("workId:"+workId);
        System.out.println("roleId:"+roleId);
        try {
            UserInfo user=new UserInfo();
            user.setRoleId(roleId);
            user.setWorkId(workId);
//            userMapper.updateById(user);
            userInfoService.updateById(user);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "用户角色更新失败！");
        }

    }

    //注册新用户
    @PostMapping("/addNewUser")
    public ResultCode addNewUser(@RequestBody UserInfo user) {
        try {//需要service业务逻辑层来判断
            boolean result=userInfoService.addNewUser(user);
            if(result) {
                return ResultCode.success();
            }else {
                return ResultCode.error(900, "该用户已创建,不能重复创建");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "添加失败");
        }
    }

    //查询发布人员
    @PostMapping("/getWorkId")
    public ResultCode getWorkId(){
        List<UserInfo> workList =userInfoService.selectList();
        return ResultCode.success(workList);
    }

}

